﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Lib.Bases;
using Lib.Utils;

namespace MS.INV.Inventory
{
    public partial class PhieuXuat_New : Lib.Bases.Forms.FBaseEdit
    {
        SysObj.Kho_PhieuXuat cls = new SysObj.Kho_PhieuXuat();
        DataTable DT_HH = new DataTable();

        public PhieuXuat_New(int _ObjID)
        {
            InitializeComponent();

            this._BaseClass = cls;

            this.ObjID = _ObjID;
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            try
            {
                using (SysObj.L_HangHoa hh = new SysObj.L_HangHoa())
                    DT_HH = hh.LoadData();
                Lib_HH.SetDataSource(DT_HH, "MaHang,TenHang#Mã hàng,Tên hàng");

                using (SysLst.L_DVT dvt = new SysLst.L_DVT())
                    repDVT.SetDataSource(dvt.LoadData(), "TenDVT#Tên");

                customerUs1.LoadData();

                if (this.ObjID > 0)
                {
                    cls.IdPhieuXuat = this.ObjID;
                    cls.ReadData();

                    customerUs1.IdKhachHang = cls.IdKhachHang;
                    txtDG.Text = cls.DienGiai;
                    txtSo.Text = cls.So;
                    dtNgay.EditValue = cls.Ngay;
                    txtNN.Text = cls.NguoiXuat;

                    grid.DataSource = cls.Get_ThucXuat();
                }
                else T_NewItemClick(null, null);
            }
            catch (Exception ex)
            {
                Lib.Bases.SysError.RaiseError(ex, this.Name, "Form load.");
            }
        }

        public override void T_NewItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            base.T_NewItemClick(sender, e);
            grid.DataSource = cls.Get_ThucXuat();
            txtSo.Text = cls.GetNo(Convert.ToDateTime(dtNgay.EditValue));
        }

        public override void T_UpdateItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                if (customerUs1.IdKhachHang <= 0)
                {
                    Msg.ErrOK("Vui lòng chọn khách hàng.");
                    return;
                }
                if (txtSo.Text.Trim().Length <= 0)
                {
                    Msg.ErrOK("Vui lòng nhập số chứng từ.");
                    return;
                }
                if (object.Equals(dtNgay.EditValue, null))
                {
                    Msg.ErrOK("Vui lòng chọn ngày lập chứng từ.");
                    return;
                }

                cls.So = txtSo.Text;
                cls.Ngay = Convert.ToDateTime(dtNgay.EditValue);
                cls.NguoiXuat = txtNN.Text;
                cls.IdKhachHang = customerUs1.IdKhachHang;
                cls.DienGiai = txtDG.Text;

                cls.D_TX = grid.GetAcceptChanges();

                base.T_UpdateItemClick(sender, e);
            }
            catch (Exception ex)
            {
                SysError.RaiseError(ex, this.Name, "Update data");
            }
        }

        public override void T_DelItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                base.T_DelItemClick(sender, e);
            }
            catch (Exception ex)
            {
                SysError.RaiseError(ex, this.Name, "Delete data");
            }
        }

        private void view_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            DataRow row = view.GetFocusedDataRow();
            if (object.Equals(row, null)) return;

            if (e.Column.FieldName.ToLower() == "idhanghoa")
            {
                DataRow[] rS = DT_HH.Select("IdHangHoa=" + Cvert.ToInt32(row["IdHangHoa"], 0));
                if (!object.Equals(rS, null) && rS.Length > 0)
                {
                    row["IdDVT"] = Cvert.ToInt32(rS[0]["idDVT"], 0);
                }
            }
            else if (e.Column.FieldName.ToLower() == "soluong" || e.Column.FieldName.ToLower() == "dongia")
                CalcRow(row);
        }

        private void CalcRow(DataRow row)
        {
            row["ThanhTien"] = Cvert.ToDecimal(row["SoLuong"], 0) * Cvert.ToDecimal(row["DonGia"], 0);

            view.PostEditor();
            view.UpdateCurrentRow();
            view.UpdateTotalSummary();
        }

        public override void T_PrintItemClick(string ReportName)
        {
            DataTable DT = cls.GetDataPrint();

            Lib.Bases.Reports.ShowReport(ReportName, DT, "", "");
        }

        private bool IsExists(DataTable Data, DataRow ro)
        {
            string sF = "IdHangHoa=" + Cvert.ToInt32(ro["IdHangHoa"], 0);
            DataRow[] rS = Data.Select(sF);
            if (!object.Equals(rS, null) && rS.Length > 0) return true;
            return false;
        }

        private void dtNgay_EditValueChanged(object sender, EventArgs e)
        {
            if (cls.IdPhieuXuat <= 0)
            {
                txtSo.Text = cls.GetNo(Convert.ToDateTime(dtNgay.EditValue));
            }
        }

        private void CopyRow(DataRow Row, DataRow ToRow)
        {
            foreach (DataColumn dc in ToRow.Table.Columns)
                if (Row.Table.Columns.Contains(dc.ColumnName))
                    ToRow[dc.ColumnName] = Row[dc.ColumnName];
        }
    }
}
